package com.coolweather.android;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.coolweather.android.db.Province;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Province> provinceList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createDataBase = (Button) findViewById(R.id.create_database);
        Button insertData = (Button) findViewById(R.id.insert_data);
        Button deleteData = (Button) findViewById(R.id.delete_data);
        createDataBase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("触发", "info");
                Connector.getDatabase();
            }
        });
        insertData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Province addProvince = new Province();
                addProvince.setProvinceCode(11);
                addProvince.setProvinceName("四川");
                addProvince.save();
                Log.d("触发添加一条数据", "info");
                provinceList = DataSupport.findAll(Province.class);
                Log.d("Province数据表中有" + String.valueOf(provinceList.size()) + "条数据", "info");
                for (Province item : provinceList) {
                    Log.d("MainActivity", "Id:" + item.getId());
                    Log.d("MainActivity", "ProvinceCode:" + item.getProvinceCode());
                    Log.d("MainActivity", "ProvinceName:" + item.getProvinceName());
                }
            }
        });
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("触发请空数据", "info");
                DataSupport.deleteAll(Province.class);
                provinceList = DataSupport.findAll(Province.class);
                Log.d("MainActivity", "Province数据表中有" + String.valueOf(provinceList.size()) + "条数据");
            }
        });
    }
}